<?php
/**
 * --------------------------------------------------------------
 * 搜索SS控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\admin\model\Document as DocumentModel;
use app\admin\model\ManualDoc as ManualDocModel;

class Search extends HomeBase
{
    /**
     * 搜索
     * 这里搜索直接采用like，实际应用中大数据量请采用其他方式
     */
    public function index()
    {
        $keywords = input('param.keywords/s', '');
        $type_id  = input('param.type_id/d', 1);

        $map = [];
        if ($type_id == 1) {
            $map[] = ['document.status', '=', 1];
            $map[] = ["document.title|document.description", 'like', "%{$keywords}%"];

            $list = DocumentModel::view('document', 'id,title,description,tags,clicks,cover,create_time')
                    ->view("column", 'title as column_title,route_name as column_route_name', 'column.id = document.cid', 'left')
                    ->where($map)
                    ->order('document.update_time desc')
                    ->paginate(15);
        } elseif ($type_id == 2) {
            $map[] = ['manual_doc.status', '=', 1];
            $map[] = ["manual_doc.doc_name", 'like', "%{$keywords}%"];

            $list = ManualDocModel::view('manual_doc', "id,doc_name as title,clicks,create_time")
                    ->view("manual", 'title as column_title,route_name as column_route_name', 'manual.id = manual_doc.manual_id', 'left')
                    ->where($map)
                    ->order('manual_doc.update_time desc')
                    ->paginate(15);
        }

        if ($list) foreach($list as $k => &$v) {
            $v['title'] = replace_keywords($v['title'], $keywords);
            if (!empty($v['column_route_name'])) {
                $v['column_url'] = ($type_id == 1) ? url('/article/'.$v['column_route_name']) : url('/manual/'.$v['column_route_name'].'/'.$v['id']);
            } else {
                $v['column_url'] = ($type_id == 1) ? url('/article/list_'.$v['cid']) : '';
            }
            if (isset($v['description']) && !empty($v['description'])) {
                $v['description'] = replace_keywords($v['description'], $keywords);
            }
        }

        $this->assign([
            'title'    => '搜索：' . $keywords . config('app.site_setting.web_site_title'),
            'keywords' => $keywords,
            'type_id'  => $type_id,
            'list'     => $list
        ]);
        return $this->fetch();
    }

    /**
     * tag
     */
    public function tag()
    {
        $tag_name = input('param.tag_name/s', '');

        $map[] = ['document.status', '=', 1];
        $map[] = ["document.tags", 'like', "%{$tag_name}%"];

            $list = DocumentModel::view('document', 'id,title,description,tags,clicks,cover,create_time')
                    ->view("column", 'title as column_title,route_name as column_route_name', 'column.id = document.cid', 'left')
                    ->where($map)
                    ->order('document.update_time desc')
                    ->paginate(15);

        if ($list) foreach($list as $k => &$v) {
            $v['tags']       = replace_keywords($v['tags'], $tag_name);
            $v['column_url'] = $v['column_route_name'] ? url('/article/'.$v['column_route_name']) : url('/article/list_'.$v['cid']);
        }

        $this->assign([
            'title' => 'tag：' . $tag_name . config('app.site_setting.web_site_title'),
            'list'  => $list
        ]);
        return $this->fetch();
    }
}